<?php
if(isSet($_SESSION[username])){
//Sistem Achievement
	$data = mysql_query("SELECT COUNT(username) AS jml, username FROM berita WHERE username='$_SESSION[username]' GROUP BY username");
	while($d = mysql_fetch_array($data)){
		/*Melakukan posting sekali*/
		if($d[jml] == 1){
			$data = mysql_query("SELECT * FROM achievement WHERE isi=1");
			$num = mysql_num_rows($data);
			if($num == 0){
				?>
				<script>
				window.alert("SELAMAT ANDA MENDAPATKAN ACHIEVEMENT 'Melakukan posting sekali'");
                </script>
				<?php
				mysql_query("INSERT INTO achievement(username, isi) VALUES ('$d[username]',1)");
			}
		}
		/*Telah melakukan posting 10 kali*/
		if($d[jml] == 10){
			$data = mysql_query("SELECT * FROM achievement WHERE isi=2");
			$num = mysql_num_rows($data);
			if($num == 0){
				?>
				<script>
				window.alert("SELAMAT ANDA MENDAPATKAN ACHIEVEMENT 'Telah melakukan posting 10 kali'");
                </script>
				<?php
				mysql_query("INSERT INTO achievement(username, isi) VALUES ('$d[username]',2)");
			}
		}
	}
	
	$data = mysql_query("SELECT COUNT(komentator) AS jml, komentator  FROM komentar WHERE komentator='$_SESSION[username]' GROUP BY komentator");
	while($d = mysql_fetch_array($data)){
		/*Berkomentar sekali*/
		if($d[jml] == 1){
			$data = mysql_query("SELECT * FROM achievement WHERE isi=3");
			$num = mysql_num_rows($data);
			if($num == 0){
				?>
				<script>
				window.alert("SELAMAT ANDA MENDAPATKAN ACHIEVEMENT 'Berkomentar sekali'");
                </script>
				<?php
				mysql_query("INSERT INTO achievement(username, isi) VALUES ('$d[komentator]',3)");
			}
		}
		/*Telah berkomentar 20 kali*/
		if($d[jml] == 20){
			$data = mysql_query("SELECT * FROM achievement WHERE isi=4");
			$num = mysql_num_rows($data);
			if($num == 0){
				?>
				<script>
				window.alert("SELAMAT ANDA MENDAPATKAN ACHIEVEMENT 'Telah berkomentar 20 kali'");
                </script>
				<?php
				mysql_query("INSERT INTO achievement(username, isi) VALUES ('$d[komentator]',4)");
			}
		}
	}
	/*Ada setidaknya satu kontennya yang telah pernah di like oleh 100 pengguna lain*/
	$data = mysql_query("SELECT count(tabel1.username) AS jml, tabel1.id_berita AS id_berita
						FROM
							(SELECT DISTINCT username, id_berita from likes) AS tabel1
						WHERE username='$_SESSION[username]'
						GROUP BY id_berita");
	while($d = mysql_fetch_array($data)){
		$user = mysql_query("SELECT username FROM berita WHERE id_berita='$d[id_berita]'");
		$s = mysql_fetch_array($user);
		if($d[jml] == 2){
			$data = mysql_query("SELECT * FROM achievement WHERE isi=5");
			$num = mysql_num_rows($data);
			if($num == 0){
				?>
				<script>
				window.alert("SELAMAT ANDA MENDAPATKAN ACHIEVEMENT 'Ada setidaknya satu kontennya yang telah pernah di like oleh 100 pengguna lain'");
                </script>
				<?php
				mysql_query("INSERT INTO achievement(username, isi) VALUES ('$s[username]',5)");
			}
		}
	}
	
	/*Ada setidaknya satu kontennya yang telah pernah di dislike 100 pengguna lain*/
	$data = mysql_query("SELECT count(tabel1.username) AS jml, tabel1.id_berita AS id_berita
						FROM
							(SELECT DISTINCT username, id_berita from dislike) AS tabel1
						WHERE username='$_SESSION[username]'
						GROUP BY id_berita");
	while($d = mysql_fetch_array($data)){
		$user = mysql_query("SELECT username FROM berita WHERE id_berita='$d[id_berita]'");
		$s = mysql_fetch_array($user);
		if($d[jml] == 2){
			$data = mysql_query("SELECT * FROM achievement WHERE isi=6");
			$num = mysql_num_rows($data);
			if($num == 0){
				?>
				<script>
				window.alert("SELAMAT ANDA MENDAPATKAN ACHIEVEMENT 'Ada setidaknya satu kontennya yang telah pernah di dislike 100 pengguna lain'");
                </script>
				<?php
				mysql_query("INSERT INTO achievement(username, isi) VALUES ('$s[username]',6)");
			}
		}
	}
}
?>